// pages/resultype/eyesight/eyesight.js
const jsonData = require("../jsondata.js");
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		stage:'',
		//筛查ID
		scid: '',
		//筛查客户ID
		customerid: '',
		//类别ID
		categoryid: '',
		//潜在客户ID
		leadid: '',
		//提交模式或修改模式
		addOrMod: 'add',
		leadName: '',
		optionShow: false,
		optionList: jsonData.optionList,

		selectType: 1,
		schoolId: '',
		/*** 视力检查* */
		//裸眼视力 右
		nakedEyeRight: {
			new_scid: null,
			new_categoryid: 'LB0007',
			new_itemid: 'XM0014',
			new_contentid: 'NR0023',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//裸眼视力 左
		nakedEyeLeft: {
			new_scid: null,
			new_categoryid: 'LB0007',
			new_itemid: 'XM0014',
			new_contentid: 'NR0025',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//矫正视力 右
		correctRight: {
			new_scid: null,
			new_categoryid: 'LB0007',
			new_itemid: 'XM0010',
			new_contentid: 'NR0024',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//矫正视力 左
		correctLeft: {
			new_scid: null,
			new_categoryid: 'LB0007',
			new_itemid: 'XM0010',
			new_contentid: 'NR0026',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//正常、异常
		eyeResult: {
			new_scid: null,
			new_categoryid: 'LB0012',
			new_itemid: 'XM0062',
			new_contentid: 'NR0172',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		var options = options;
		console.log(options);
		if (options) {
			if(options.stage){
				this.setData({
					stage: options.stage
				})
			}
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			wx.showLoading({
				title: '加载中...',
				mask: true,
			})
			var result = await this.GetScreenResultByCategory(options.scid, options.sccustid, options.categoryid);
			if (options.schoolId) {
				this.setData({
					schoolId: options.schoolId
				});
			}
			this.sortscResult(result);
			wx.hideLoading();
		}


	},
	//处理筛查结果
	sortscResult: function(result) {
		var that = this;
		if (result.data.length > 0) {
			this.setData({
				addOrMod: 'mod'
			});
			result.data.forEach(item => {
				//裸眼视力 右
				if (item.new_categoryid == "LB0007" && item.new_itemid == "XM0014" && item.new_contentid == "NR0023") {
					that.setData({
						nakedEyeRight: item
					})
				}
				//裸眼视力 左
				else if (item.new_categoryid == "LB0007" && item.new_itemid == "XM0014" && item.new_contentid == "NR0025") {
					that.setData({
						nakedEyeLeft: item
					})
				}
				//矫正视力 右
				else if (item.new_categoryid == "LB0007" && item.new_itemid == "XM0010" && item.new_contentid == "NR0024") {
					that.setData({
						correctRight: item
					})
				}
				//矫正视力 左
				else if (item.new_categoryid == "LB0007" && item.new_itemid == "XM0010" && item.new_contentid == "NR0026") {
					that.setData({
						correctLeft: item
					})
				}

			})
		}
	},
	//提交视力检查
	eyesightSubmit: function(e) {
		if (!this.checkEmpty()) {
			wx.showToast({
				title: '请填写裸眼视力',
				icon: 'none'
			})
			return false;
		}
		var that = this;
		var screenResultRequest = {}
		var createResult = this.getEyesight();
		screenResultRequest.screenResults = createResult;
		screenResultRequest.screenCustomer = {
			new_customerid: this.data.customerid,
			new_scid: this.data.scid
		}
		commonutil.createScreenResultsSchool(screenResultRequest, function(res) {
			var idArray = res.result.split('|');
			for (var i = 0; i < idArray.length; i++) {
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				nakedEyeRight: createResult[0],
				nakedEyeLeft: createResult[1],
				correctRight: createResult[2],
				correctLeft: createResult[3],
			});
		});
	},
	//更新
	eyesightUpdate: function() {
    if (!this.checkEmpty()) {
      wx.showToast({
        title: '请填写裸眼视力',
        icon: 'none'
      })
      return false;
    }
		var updateResult = this.getEyesight();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
		var url = "/pages/print/letter/letter?scid=" + scid;
		if (this.data.schoolId)
			url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//视力检查
	getEyesight: function() {
		var createResult = [];
		var nakedEyeLeft = this.data.nakedEyeLeft;
		var nakedEyeRight = this.data.nakedEyeRight;
		var correctRight = this.data.correctRight;
		var correctLeft = this.data.correctLeft;
		var eyeResult = this.data.eyeResult;
		var scid = this.data.scid;
		var customerid = this.data.customerid;

    var stage = this.data.stage;
    if(stage!=''){ //中小学校园  非中小学校园不需要 判断 视力结果
      if(nakedEyeLeft.new_result=='无光感'||nakedEyeLeft.new_result=='手动'||nakedEyeLeft.new_result=='指数'
      ||nakedEyeRight.new_result=='光感'||nakedEyeLeft.new_result=='光感'
      ||nakedEyeRight.new_result=='无光感'||nakedEyeRight.new_result=='手动'||nakedEyeRight.new_result=='指数'){
        eyeResult.new_result = 'SC0000000005';
      }else{
        var signLeft = parseFloat(nakedEyeLeft.new_result);
        var signRight = parseFloat(nakedEyeRight.new_result);
        
        if(stage=='1'){ //幼儿园
        //2020/4/20 正常还是异常的逻辑需要改成 中小学: 双眼裸眼视力>=4.9 为正常; 幼儿园: 双眼裸眼视力:3岁>=4.7;4-5岁>=4.8;6岁>=4.9为正常
        //待修改
          let age = this.data.age;
          if((age>=6) && (signLeft<4.9 || signRight<4.9)){
            eyeResult.new_result = 'SC0000000005';
          }
          
          if((age>=4 && age<=5) && (signLeft<4.8 || signRight<4.8)){
            eyeResult.new_result = 'SC0000000005';
          }
          if ((age>=0 && age <= 3)  && (signLeft < 4.7 || signRight < 4.7)) {
            eyeResult.new_result = 'SC0000000005';
          }
          // if (signLeft < 4.8 || signLeft > 4.9||signRight < 4.8 || signRight > 4.9) {
          //   eyeResult.new_result = 'SC0000000005';
          // }
        }else{ //中小学
          if (signLeft < 4.9 || signRight < 4.9) {
            eyeResult.new_result = 'SC0000000005';
          }
        }



      }
      createResult.push(eyeResult); 
    }
		createResult.push(nakedEyeRight);
		createResult.push(nakedEyeLeft);
		if (correctRight.new_result&&correctRight.new_result != '' && correctRight.new_result != '请填写') {
			createResult.push(correctRight);
		}
		if (correctLeft.new_result&&correctLeft.new_result != '' && correctLeft.new_result != '请填写') {
			createResult.push(correctLeft);
		}		
		
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	},
	/**视力值表的显示 */
	openOption: function(e) {
		this.setData({
			optionShow: true,
			selectType: e.currentTarget.dataset.type
		})
	},
	//选择结果
	onSelect: function(e) {
		//console.log(e);
		if (this.data.selectType == 1) {
			this.setData({
				['nakedEyeRight.new_result']: e.currentTarget.dataset.name,
				//nakedEyeRightId: e.currentTarget.id,
			});
		} else if (this.data.selectType == 2) {
			this.setData({
				['nakedEyeLeft.new_result']: e.currentTarget.dataset.name,
				//nakedEyeLeftId: e.currentTarget.id,
			});
		} else if (this.data.selectType == 3) {
			this.setData({
				['correctRight.new_result']: e.currentTarget.dataset.name,
				//correctRightId: e.currentTarget.id,
			});
		} else if (this.data.selectType == 4) {
			this.setData({
				['correctLeft.new_result']: e.currentTarget.dataset.name,
				//correctLeftId: e.currentTarget.id,
			});
		}
		this.setData({
			optionShow: false
		});
	},
	//清空按钮
	onClear: function() {
		if (this.data.selectType == 1) {
			this.setData({
				['nakedEyeRight.new_result']: '请填写',
				// nakedEyeRightId: 0
			});
		} else if (this.data.selectType == 2) {
			this.setData({
				['nakedEyeLeft.new_result']: '请填写',
				// nakedEyeLeftId: 0
			});
		} else if (this.data.selectType == 3) {
			this.setData({
				['correctRight.new_result']: '请填写',
				// correctRightId: 0
			});
		} else if (this.data.selectType == 4) {
			this.setData({
				['correctLeft.new_result']: '请填写',
				// correctLeftId: 0
			});
		}
		this.setData({
			optionShow: false
		});
	},
	onClose: function() {
		this.setData({
			optionShow: false
		});
	},
	//获取筛查结果
	GetScreenResultByCategory: async function(scid, customerid, categoryid) {
		var result = screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		return result
	},
	checkEmpty: function() {
		var nakedEyeLeft = this.data.nakedEyeLeft;
		var nakedEyeRight = this.data.nakedEyeRight;
		var correctRight = this.data.correctRight;
		var correctLeft = this.data.correctLeft;
    if (nakedEyeLeft.new_result == '' || nakedEyeLeft.new_result == '请填写') {
			return false;
		}
    if (nakedEyeRight.new_result == '' || nakedEyeRight.new_result == '请填写') {
			return false;
	}
	
		return true;
	}
})
